package com.sys.servlet;

import com.sys.dao.sysDao;
import com.sys.data.code;
import com.sys.util.console;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

import static com.sys.util.stringUtils.StringIsEmptyOrNull;

@WebServlet(name = "addItemDiskServlet", urlPatterns = "/sys/addItemDisk", description = "添加盘损")
public class addItemDiskServlet extends sysServlet {

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    setHeadHtml(req, resp, "addItemDiskServlet");
    sysDao dao = new sysDao();
    PrintWriter out = resp.getWriter();
    JSONObject backJson = new JSONObject();

    String diskItemRecordUserName = req.getParameter("diskItemRecordUserName");
    String diskItemRecordItemName = req.getParameter("diskItemRecordItemName");
    String diskItemRecordItemSpe = req.getParameter("diskItemRecordItemSpe");
    String diskItemRecordItemType = req.getParameter("diskItemRecordItemType");
    if (StringIsEmptyOrNull(diskItemRecordUserName) || StringIsEmptyOrNull(diskItemRecordItemName) || StringIsEmptyOrNull(diskItemRecordItemSpe) ||
      StringIsEmptyOrNull(diskItemRecordItemType)) {
      backJson.put("code", code.ErrorParam.value());
      backJson.put("msg", code.ErrorParam.msg());
      out.print(backJson);
      return;
    }

    int diskItemItemCount;
    try {
      diskItemItemCount = Integer.parseInt(req.getParameter("diskItemItemCount"));
    } catch (Exception e) {
      e.printStackTrace();
      console.log("盘损物品数量错误");
      backJson.put("code", code.ErrorParam.value());
      backJson.put("msg", code.ErrorParam.msg());
      out.print(backJson);
      return;
    }

    dao.addItemDisk(diskItemRecordUserName, diskItemRecordItemName, diskItemRecordItemSpe, diskItemRecordItemType, diskItemItemCount, out::print);
  }
}
